﻿var evImgNolines_minus = "<img src='JS/SimpleTree/img/nolines_minus.gif' class='evImg' onclick=closeNode('@NodeID') />";
var evImgNolines_plus = "<img src='JS/SimpleTree/img/nolines_plus.gif' class='evImg' onclick=openNode('@NodeID') />";
var newNode = "<div  id='@ID' parentId='@PID' type='@type' isLoad='false' level=@level><span class='spanImgList'></span><span style='cursor:pointer' type='activeHeader' class='noSub' onclick=ShowNode('@ID')></span><img src='@ImgURL' style='cursor:pointer'  ondblclick=\"forceLoad(new TreeNode('@ID'))\"  /><a href=\"@URL\" target='formframe' onclick=\"TreeNodeClick(new TreeNode('@ID'))\" ><span class='Title'> @Title</span>&nbsp;&nbsp;<span class='SubCount'></span></a></div>";

var OisTreePanel


function OISTree(Id, Title, IcoPath, LocationID, type, url) {
    debugger;
    var divRoot = document.createElement("div");
    var divLocation= document.getElementById(LocationID);
    //divLocation.innerHTML = "";

    divLocation.appendChild(divRoot);
    
    OisTreePanel = LocationID;

    var newNodeStr = newNode;
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);

    newNodeStr = newNodeStr.replace("@PID", "Null");
    newNodeStr = newNodeStr.replace("@Title", Title);
    newNodeStr = newNodeStr.replace("@level", 1);
    newNodeStr = newNodeStr.replace("@ImgURL", IcoPath);
    newNodeStr = newNodeStr.replace("@type", type);
    newNodeStr = newNodeStr.replace("@URL", url);


    $(divRoot).html(newNodeStr);

    this.addNode = addNode;

    //this.TreeNodeClick = new function() { };

    //        <div style="width:200px; font-size:18px;" id="treeOne">
    //             <div style="width:200px;" id="treeOne_1" parentId="Null"level=1><span type="activeHeader" class="noSub" onclick="ShowNode('treeOne_1')" ></span><img src="img/globe.gif" />国家</div>
    //         </div>
}

function TreeNode(NodeId) {
    var node = document.getElementById(NodeId);
    this.getTitle = function() {
        return $("#" + NodeId).find("span[class=Title]")[0].innerHTML;
    }

    this.getID = function() {
        return NodeId;  
    }

    this.getParentNode = function() {
        return new TreeNode(node.parentId);
    }

    this.getType = function() {
        return $("#" + NodeId)[0].type;
    }
    this.getIsLoad = function() {
        return $("#" + NodeId)[0].isLoad;
    }



    //span class='Title'
}

function setIsLoad(nodeId) {
    var obj = document.getElementById(nodeId)
    if (obj != undefined) {
        obj.isLoad = "true";
    }
}



/*添加新节点
Id 当前节点的 ID
Pid 父节点的 ID
Title 显示的标题
IcoPath 图片路径
*/
function addNode(Id, Pid, Title, IcoPath, type, url) {
    var ParentNode = document.getElementById(Pid)
    var parentLevel = $("#" + Pid)[0].level * 1;

    var newDiv = document.createElement("div");

    var LastNode = getFinalLastNode(ParentNode);

    document.getElementById(OisTreePanel).insertBefore(newDiv, LastNode.nextSibling);


    //获取全局的新节点字符串，并替换其中的标记。
    var newNodeStr = newNode;
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);
    newNodeStr = newNodeStr.replace("@ID", Id);

    newNodeStr = newNodeStr.replace("@PID", Pid);
    newNodeStr = newNodeStr.replace("@Title", Title);
    newNodeStr = newNodeStr.replace("@level", parentLevel + 1);
    newNodeStr = newNodeStr.replace("@ImgURL", IcoPath);
    newNodeStr = newNodeStr.replace("@type", type);


    if (url == undefined || url == null) {
        newNodeStr = newNodeStr.replace("href=\"@URL\"", "");
        newNodeStr = newNodeStr.replace("style='cursor:pointer'  ondblclick", "ondblclick");
       
    }
    else {
        newNodeStr = newNodeStr.replace("@URL", url);
    }
    
    $(newDiv).html(newNodeStr);

    updateParentNode(ParentNode);
    //updatePreviousNode(LastNode);


    RefreshSubNode(Pid);

}

function ShowSubNode(nodeId, isShow) {
    var nodeColl = $("div[parentId=" + nodeId + "]")
    nodeColl.each(function(i) {
        if (isShow) {
            this.style.display = "block";
        }
        else {
            this.style.display = "none";
        }
        if (this.outerHTML.indexOf("minus") > 0) {
            ShowSubNode(this.id, isShow);
        }
    })
}


function ShowNode(nodeId) {
    var span1 = $("#" + nodeId).find("span[type=activeHeader]")[0];
    if (span1.outerHTML.indexOf("plus") > 0) {
        //将加号替换为减号,样式不变
        $(span1).html(span1.outerHTML.replace("plus", "minus"));
        ShowSubNode(nodeId, true);
        
        TreeNodeLoad(new TreeNode(nodeId),false);
    }
    else if (span1.outerHTML.indexOf("minus") > 0) {
        //减号替换为加号,样式不变
        $(span1).html(span1.outerHTML.replace("minus", "plus"));
        ShowSubNode(nodeId, false);
    }
    else if (span1.outerHTML.indexOf("noSub") > 0) {
        //加载下级节点
    //TreeNodeLoad(new TreeNode(nodeId));
    }

}

function CloseNode(nodeId) {
    
    var span1 = $("#" + nodeId).find("span[type=activeHeader]")[0];
    var node = document.getElementById(nodeId);
    var SubnodeColl = $("div[parentId=" + nodeId + "]")
    if (SubnodeColl.length == 0) {
        $(span1).html(span1.outerHTML.replace("plus", "minus"));
        $(span1).html(span1.outerHTML.replace("noSub", "minus"));
    }
    else {
        $(span1).html(span1.outerHTML.replace("minus","plus"));
        ShowSubNode(nodeId, false);
    }
}



//删除父节点下面的所有节点。
function removeSubNode(parentId) {
    var nodeColl = $("div[parentId=" + parentId + "]")
    nodeColl.each(function(i) {
        removeNode(this.id);
    });
}

//删除当前节点以及下面的所有节点。
function removeNode(nodeId) {
    var nodeColl = $("div[parentId=" + nodeId + "]")
    nodeColl.each(function(i) {
        removeNode(this.id);
    });

    var parent = document.getElementById("divTree");
    var sub = document.getElementById(nodeId);
    parent.removeChild(sub);
}



//用于插入节点时调用.
//将新节点插入到此节点的 nextSibling 之前，
function getFinalLastNode(ParentNode) {
    var subNodeColl = $("div[parentId=" + ParentNode.id + "]");
    if (subNodeColl.length <= 0) {
        //如果需要插入的父节点下面没有子节点，则直接返回此父节点。
        return ParentNode;
    }
    else {
        //否则使用此父节点下面的最后一个再递归调用此方法。
        //注意它的上面兄弟节点下面可能已经有很多子子孙孙的节点了。所以要递归找到最下面的一个节点。
        return getFinalLastNode(subNodeColl[subNodeColl.length - 1]);
    }
}

//刷新子节点。
//当插入一个新节点的时候，需要刷新此节点的父节点下面的所有节点。
function RefreshSubNode(parentNodeId) {
    var nodeColl = $("div[parentId=" + parentNodeId + "]")
    nodeColl.each(function(i) {
        RefreshNode(this.id); //更新当前节点。
        RefreshSubNode(this.id);  //递归调用。刷新此节点下面的子节点。
    })

}

//刷新此节点
function RefreshNode(currNodeId) {

    var imgList = getImgList(currNodeId);
    var Img1 = $("#" + currNodeId).find("span[class=spanImgList]")[0];
    $(Img1).html(imgList);

}



//更新父节点。
//原先父节点下面无子节，现在有节点子，需要增加一个收缩的开头按钮。
//此方法还需要完善，应根据具体情况加不同的按钮，如果改的话， RefreshNode 方法也需要改变。
//或者可以把 updateParentNode 的逻辑移到 RefreshNode 当中.
function updateParentNode(ParentNode) {
    var subNodeColl = $("div[parentId=" + ParentNode.id + "]");
    if (subNodeColl.length > 0) {
        if (ParentNode.innerHTML.indexOf("noSub") >= 0) {
            ParentNode.innerHTML = ParentNode.innerHTML.replace("noSub", "nolines_minus");
        }
    }
    else {
        //移出节点时需要调用的逻辑。
        //替换为class="noSub" 使用正则
    }
}

function setNodeIsOpen(nodeId) {
    var node = document.getElementById(nodeId);
    var html = node.outerHTML;
    $(node).html(html.replace("noSub", "nolines_minus"));
}



//得到当前节点前面应该显示的线条。
function getImgList(currNodeId) {

    var currNode = document.getElementById(currNodeId);
    var hasSub = false;
    var hasBrother = false;
    var subNodeColl = $("div[parentId=" + currNodeId + "]");
    if (subNodeColl.length > 0) {
        //存在下线节点。
        hasSub = true;
    }
    var imgList = "";

    subNodeColl = $("div[parentId=" + currNode.parentId + "]");
    //父节点的最后一个子节点不是当前节点，则说明当前节点下面还有兄弟节点。
    if (subNodeColl[subNodeColl.length - 1].id != currNode.id) {
        hasBrother = true;
        imgList = "<img src='JS/SimpleTree/img/join.gif' />";
    }
    else {
        imgList = "<img src='JS/SimpleTree/img/joinbottom.gif' />";
    }

    currNode = document.getElementById(currNode.parentId);
    var tempParentNode = document.getElementById(currNode.parentId);
    var tempSubNode = document.getElementById(currNode.id);

    while (tempParentNode != null) {
        subNodeColl = $("div[parentId=" + tempParentNode.id + "]");
        if (subNodeColl[subNodeColl.length - 1].id != tempSubNode.id) {
            imgList = "<img src='JS/SimpleTree/img/line.gif' />" + imgList;
        }
        else {
            imgList = "<img src='JS/SimpleTree/img/empty.gif' />" + imgList;
        }
        tempSubNode = tempParentNode;
        tempParentNode = document.getElementById(tempParentNode.parentId);
    }

    imgList = "<span class='spanImgList'>" + imgList + "</span>";

    return imgList;


    var imgList = "<img src='JS/SimpleTree/img/empty.gif' />";
    for (var i = 1; i < level; i++) {
        imgList = "<img src='JS/SimpleTree/img/empty.gif' />" + imgList;
    }
    return imgList;
}
        